/* Copyright (C) 2010 MoSync AB

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/

#ifndef _GRID_MENU_H_
#define _GRID_MENU_H_

#include <MAUI/Layout.h>
#include <MAUI/Widget.h>
#include <MAUtil/Environment.h>
#include <MAUtil/Set.h>

using namespace MAUI;

//Interface class to call back to
class GridMenuListener
{
	public:
		virtual void GridMenuItemSelected(int item, Widget* selectedItem) = 0;
};

class GridMenu : public Layout, public KeyListener, public PointerListener
{
	public:
		GridMenu(int x, int y, int width, int height, Widget* parent = NULL, int rows = 3, int columns = 3);
		~GridMenu();

		void pointerPressEvent(MAPoint2d point);
		void pointerMoveEvent(MAPoint2d point);
		void pointerReleaseEvent(MAPoint2d point);
		void keyPressEvent(int keyCode);
		void keyReleaseEvent(int keyCode);

		bool handleEvents;

		void add(Widget* button);

		void addListener(GridMenuListener* l);

		void setSelectedIndex(int index);
		int getSelectedIndex();

		void selectNext();
		void selectPrevious();
		void selectNextRow();
		void selectPreviousRow();
		
		void setEnabled(bool e);
		
		void drawWidget();

		void bindKeyLeft(int keyCode);
		void bindKeyRight(int keyCode);
		void bindKeyUp(int keyCode);
		void bindKeyDown(int keyCode);
		void bindKeySelect(int keyCode);

		void setPanelActive(int panelID, bool active = true);
	private:
		GridMenuListener* listener;
		Widget* selectedWidget;
		Widget* previouslySelectedWidget;
		int _rows;
		int _cols;
		int itemCount;
		int selectedIndex;
		void selectButton(int index);
		void informListener();
		void locateWidget(MAPoint2d, bool inform = false);

		bool setContains(Set<int> s, int value);
		Set<int> moveUpKeys;
		Set<int> moveDownKeys;
		Set<int> moveLeftKeys;
		Set<int> moveRightKeys;
		Set<int> selectKeys;
		int origW;
		Vector<int> activePanels;
};

#endif
